package com.yhf.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yhf.mapper.DeviceGatherMapper;
import com.yhf.mapper.DeviceMapper;
import com.yhf.model.Device;
import com.yhf.model.DeviceGather;
import com.yhf.service.DeviceGatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
@Service
public class CreateDataBase {
    @Autowired
    DeviceMapper deviceMapper;
    @Autowired
    DeviceGatherService deviceGatherService;
    public void getData(){
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        List<Device> devices = deviceMapper.selectList(queryWrapper);
        List<Integer> collect = devices.stream().map(Device::getId).collect(Collectors.toList());
        Random random = new Random();
        Integer integer = collect.get(random.nextInt(collect.size()));
        Device device = devices.get(integer);
        DeviceGather deviceGather = new DeviceGather();
        deviceGather.setDeviceName(device.getName());
        deviceGather.setDevicesn(device.getSn());
        int i = random.nextInt(500);
        float i1 = i;
        deviceGather.setBasicData(i1);
        deviceGather.setGatherTime(new Date());
        deviceGatherService.save(deviceGather);
    }
}
